package org.fxcontainer.validation.validator.base
{

	import mx.validators.Validator;
	import org.fxcontainer.utils.Resource;
	import org.fxcontainer.validation.validator.utils.ValidationErrorCodes;

	public class BaseValidator extends Validator
	{

		public static const DEFAULT_VALIDATION_MESSAGES_BUNDLE:String="validation.global";

		public function BaseValidator()
		{
			super();
		}

		/**
		 * @private bundle for localization
		 */
		private var _bundle:String;

		/** @private map to convert error codes (SPValidateErrorCodes) to error message localization key */
		private var _errorCodeToMessageKeyMap:Object={};


		/** @private field Name for substitution in error messages */
		private var _fieldName:String;

		/** @private params for error message substistution [0]-fieldName [1]-field value ... */
		private var _params:Array;

		/**
		 * Bundle for localization (isn't inherited)
		 * @default DEFAULT_VALIDATION_MESSAGES_BUNDLE
		 */
		public function get bundle():String
		{
			return _bundle ? _bundle : DEFAULT_VALIDATION_MESSAGES_BUNDLE;
		}

		/**
		 * @private
		 */
		public function set bundle(value:String):void
		{
			_bundle=value;
		}

		/**
		 * map to convert error codes (SPValidateErrorCodes) to error message localization key
		 */
		public function get errorCodeToMessageKeyMap():Object
		{
			return _errorCodeToMessageKeyMap;
		}

		/**
		 * Field Name for substitution in error messages
		 */
		public function get fieldName():String
		{
			return _fieldName ? _fieldName : property;
		}

		/**
		 * @private
		 */
		public function set fieldName(value:String):void
		{
			_fieldName=value;
		}

		/**
		 * Get errorMessage for errorCode
		 */
		public function getErrorMessage(errorCode:String):String
		{
			var key:String=_errorCodeToMessageKeyMap[errorCode];
			if (null == key)
			{
				key=Resource.getString(errorCode);
			}
			if (null != key)
			{
				_errorCodeToMessageKeyMap[errorCode]=key;
			}
			if (key == null)
			{
				key=errorCode;
			}
			return key;
		}

		/**
		 * Get errorMessageKey for errorCode
		 */
		public function getErrorMessageKey(errorCode:String):String
		{
			return _errorCodeToMessageKeyMap[errorCode] ? _errorCodeToMessageKeyMap[errorCode] : errorCode;
		}

		/**
		 * params for error message substistution [0]-fieldName [1]-field value ...
		 */
		public function get params():Array
		{
			if (_params == null)
			{
				_params=[fieldName, getValueFromSource()];
			}
			else
			{
				_params[0]=fieldName;
				_params[1]=getValueFromSource();
			}
			return _params;
		}

		/**
		 * @private
		 */
		public function set params(value:Array):void
		{
			_params=value;
		}

		override public function get requiredFieldError():String
		{
			return getErrorMessage(ValidationErrorCodes.REQUIRED_FIELD_ERROR);
		}

		public function set requiredFieldErrorKey(value:String):void
		{
			setErrorMessageKey(ValidationErrorCodes.REQUIRED_FIELD_ERROR, value);
		}

		/**
		 * Set errorMessageKey for errorCode
		 */
		public function setErrorMessageKey(errorCode:String, errorMessageKey:String):void
		{
			_errorCodeToMessageKeyMap[errorCode]=errorMessageKey;
		}

	}
}